package com.example.function.integration.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.example.function.integration.enums.BizTypeEnum;
import com.example.function.integration.service.AbstractDataExportService;
import com.example.function.integration.service.AccountFileGenerationService;
import com.example.function.integration.utils.ExcelUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import static com.example.function.integration.constant.CommonConstant.*;
import static com.example.function.integration.enums.BizTypeEnum.getEnumByDesc;

/**
 * @description：账务文件生成服务接口实现类
 * @className：CreditApplyDataExportService
 * @author：ext.fengcheng
 * @date：2023/9/7 10:26
 */
@Service
public class AccountFileGenerationServiceImpl extends AbstractDataExportService implements AccountFileGenerationService {

    /**
     * @Description：根据excel文件中给定的业务场景进行生成对应的账务文件数据
     * @Author： ext.fengcheng
     * @Param： [file]
     * @Return： boolean
     */
    @Override
    public boolean generationAccountFile(MultipartFile file) {
        logger.info("generationAccountFile.开始解析业务文件");
        try {
            List<List<String>> excelToList = ExcelUtils.readExcelToList(file, 1);
            logger.info("业务文件解析数据结果：{}", JSONObject.toJSONString(excelToList));

            Map<String, Map<String, List<String>>> dataMap = getStringMapMap(excelToList);

            AtomicReference<Object> object = new AtomicReference<>(new Object());
            dataMap.forEach((key, value) -> {
                List<String> bizDateList = value.get(BIZ_DATE);
                List<String> bizTypeList = value.get(BIZ_TYPE);
                for (int i = 0; i < bizDateList.size(); i++) {
                    String desc = bizTypeList.get(i);
                    if (StringUtils.isNotBlank(desc)) {
                        String bizDate = bizDateList.get(i);
                        String bizType = bizTypeList.get(i);
                        BizTypeEnum bizTypeEnum = getEnumByDesc(desc);
                        if (ObjectUtils.isNotEmpty(bizTypeEnum)) {
                            switch (bizTypeEnum) {
                                case LEND:
                                    object.set(generationLendAccountFile(bizDate, bizType));
                                    break;
                                case LEND_PHASE_I:
                                case LEND_PHASE_II:
                                case LEND_PHASE_III:
                                case LEND_PHASE_IIII:
                                case LEND_PHASE_FIVE:
                                case LEND_PHASE_SIX:
                                case LEND_PHASE_SEVEN:
                                case LEND_PHASE_EIGHTH:
                                case LEND_PHASE_NINTH:
                                case LEND_PHASE_TEN:
                                case LEND_PHASE_ELEVENTH:
                                case LEND_PHASE_TWELVE:
                                case OVERDUE_BORROWINGS:
                                    break;
                                default:
                                    logger.error("generationAccountFile|无法处理的业务类型,desc:{}", desc);
                                    throw new RuntimeException("Unexpected desc: " + desc);
                            }
                        }
                    }
                }
            });
            logger.info("开始生成业务场景对应的账务文件：{}", object.get());
            ExcelUtils.exportTxtFile(FILE_PATH, object.get());
            logger.info("生成业务场景对应的账务文件|完成");
        } catch (Exception e) {
            logger.error("解析业务文件, 解析过程中发生异常，异常信息：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return true;
    }

    private static Map<String, Map<String, List<String>>> getStringMapMap(List<List<String>> excelToList) {
        Map<String, Map<String, List<String>>> dataMap = Maps.newHashMap();
        // 根据文件数据类型生成不同的账务文件
        for (int i = 0; i < excelToList.size(); i++) {
            List<String> list = excelToList.get(i);
            Map<String, List<String>> mapElement = Maps.newHashMap();
            List<String> bizDateList = Lists.newArrayList();
            List<String> bizTypeList = Lists.newArrayList();
            for (int i1 = 0; i1 < list.size(); i1++) {
                if (i1 == LIST_INDEX) continue;
                if (i1 % 2 == LIST_INDEX) {
                    bizTypeList.add(list.get(i1));
                } else {
                    bizDateList.add(list.get(i1));
                }
            }
            mapElement.put(BIZ_DATE, bizDateList);
            mapElement.put(BIZ_TYPE, bizTypeList);
            dataMap.put(list.get(i), mapElement);
        }
        return dataMap;
    }

    /**
     * @Description：生成放款申请账务文件数据
     * @Author： ext.fengcheng
     * @Date：2023/11/20 17:45
     * @Param： [bizDate, bizType]
     * @Return： java.lang.Object
     */
    public Object generationLendAccountFile(String bizDate, String bizType) {
        return "生成一条放款账务文件：" + bizDate + SEPARATOR + bizType;
    }
}
